package com.zgkj.Zip.service;

import java.io.File;

import com.zgkj.Zip.unzip;
import com.zgkj.Zip.form.BiTxnBaseForm;
import com.zgkj.Zip.tools.DateUtil;
import com.zgkj.Zip.tools.FileUitl;
import com.zgkj.Zip.tools.HttpClientUtil;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;


public class UploadFileService {

	 private static String urlString1 = "http://139.224.44.65:9999/lf/api/entra";

	  private static String urlString2 = "http://localhost:8888/lf/api/entra";

	  private static String urlString0 = "http://139.224.44.65:8888/lf/api/entra";

	  private static String mchntidString = unzip.mchntid;
	public static void uploadFileToSer(String filepath) throws Exception{

		BiTxnBaseForm baseForm = new BiTxnBaseForm();
		JSONObject jsonObj =  new JSONObject();
		File file = new File(filepath);

		jsonObj.put("filename", file.getName());
		jsonObj.put("inputfile", FileUitl.encodeBase64File(filepath));
		baseForm.setReqstr(jsonObj);
        baseForm.setMchntid(mchntidString);//商户号
        baseForm.setTxntype("1001");	//交易类型
        String aString = Math.random()+"";
        baseForm.setMchntseq(DateUtil.getSystemCurrentDateTime()+aString.substring(aString.length()-4,aString.length()));	//商户流水号
        JsonConfig conf = new JsonConfig();
		conf.setJsonPropertyFilter(new PropertyFilter(){
             @Override
			public boolean apply(Object arg0, String arg1, Object arg2) {
                 if (arg2==null||arg2.equals("")) {
                     return true;
                 } else {
                     return false;
                 }
             }
         });
        JSONObject json = JSONObject.fromObject(baseForm,conf);
        String urlString = "";
        //孔存飞
        if ("0".equals(unzip.istest))
            urlString = urlString0;
          else if ("1".equals(unzip.istest))
            urlString = urlString1;
          else if ("2".equals(unzip.istest)) {
            urlString = urlString2;
          }

        String getString = HttpClientUtil.sendAndRecv(urlString,"UTF-8",json.toString());
        System.out.println(getString);
	}

	public static void main(String[] args) throws Exception {
		UploadFileService UploadFileService = new UploadFileService();
		try {
			com.zgkj.Zip.service.UploadFileService.uploadFileToSer("D:/123.pdf");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
